#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include"inc/game.h"
#include"inc/app.h"
#include"inc/tem.h"
#include"inc/pthread.h"
//#include"lv_mygec_font.h"
#include"lv_conf.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>    // 配置串口的头文件
#define COM1 "/dev/ttySAC2"

lv_obj_t *main_cont;//环境检测容器
volatile int Temp = 0;
volatile int hum = 0; 
volatile int press = 0;
volatile int Lux = 0;
volatile int hei = 0;
lv_obj_t *light_label;
lv_obj_t *temp_label;
lv_obj_t *humi_label;
lv_obj_t *press_label;
lv_obj_t *alti_label;
// 定义一个普通的 C 函数，用于定时刷新 UI
static void ui_refresh_callback(lv_timer_t * t)
{
    (void)t; // 避免未使用参数警告
    update_tem_ui(); // 调用你的 UI 刷新函数
}
void exit_tem(lv_event_t *e)//退出温湿度检测
{     
   
        tem_update_requested =1;
        lv_obj_del(main_cont);
        main_cont = NULL;
      
    // 显示软件盒主界面
    lv_obj_remove_flag(app_container, LV_OBJ_FLAG_HIDDEN);
    

}
extern const lv_font_t lv_mygec_font;
void create_tem(lv_obj_t* parent) 
{
    
    // 创建主容器
    main_cont = lv_obj_create(parent);
    lv_obj_set_size(main_cont, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));
    lv_obj_set_align(main_cont, LV_ALIGN_TOP_LEFT);
    lv_obj_clear_flag(main_cont, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(main_cont, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(main_cont, 0, 0);

    // 标题
    lv_obj_t *title = lv_label_create(main_cont);
    lv_label_set_text(title, "环境监测");
    lv_obj_set_style_text_font(title, &lv_mygec_font, LV_PART_MAIN);
    lv_obj_set_style_text_color(title, lv_color_hex(0x000000), 0);
    lv_obj_set_style_text_font(title, &lv_mygec_font, 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);

   
    /* 温度标签（直接设置位置和样式） */
    temp_label = lv_label_create(main_cont);
    lv_label_set_text_fmt(temp_label, "温度: %d℃\n",Temp);
    lv_obj_set_size(temp_label, 150, 30); // 宽度150，高度30s
    lv_obj_set_style_text_font(temp_label, &lv_mygec_font, LV_PART_MAIN);
    lv_obj_set_style_text_color(temp_label, lv_color_hex(0X000000), 0);
    //lv_obj_set_style_text_font(temp_label, &lv_font_montserrat_24, 0);
    lv_obj_align(temp_label, LV_ALIGN_TOP_LEFT, 30, 80);
    
    /* 湿度标签 */
    humi_label = lv_label_create(main_cont);
    lv_label_set_text_fmt(humi_label, "湿度: %d%%\n",hum);
    lv_obj_set_size(humi_label, 150, 30);
    lv_obj_set_style_text_font(humi_label, &lv_mygec_font, LV_PART_MAIN);
    lv_obj_set_style_text_color(humi_label, lv_color_hex(0X000000), 0);
    lv_obj_align(humi_label, LV_ALIGN_TOP_RIGHT, -30, 80);
    lv_obj_set_style_text_font(humi_label, &lv_mygec_font, 0);
    /* 气压标签 */
    press_label = lv_label_create(main_cont);
    lv_label_set_text_fmt(press_label, "气压: %dPa\n",press);
    lv_obj_set_size(press_label, 150, 30);
    lv_obj_set_style_text_font(press_label, &lv_mygec_font, LV_PART_MAIN);
    lv_obj_set_style_text_color(press_label, lv_color_hex(0X000000), 0);
    lv_obj_align(press_label, LV_ALIGN_TOP_LEFT, 30, 130);
     lv_obj_set_style_text_font(press_label, &lv_mygec_font, 0);
    /* 高度标签 */
    alti_label = lv_label_create(main_cont);
    lv_label_set_text_fmt(alti_label, "海拔: %dm\n",hei);
    lv_obj_set_size(alti_label, 150, 30);
    lv_obj_set_style_text_font(alti_label, &lv_mygec_font, LV_PART_MAIN);
    lv_obj_set_style_text_color(alti_label, lv_color_hex(0X000000), 0);
    lv_obj_align(alti_label, LV_ALIGN_TOP_RIGHT, -30, 130);
     lv_obj_set_style_text_font(alti_label, &lv_mygec_font, 0);
    /* 光照标签 */
    light_label = lv_label_create(main_cont);
    lv_label_set_text_fmt(light_label, "光照强度: %dlux\n",Lux);
    lv_obj_set_size(light_label, 150, 30);
    lv_obj_set_style_text_font(light_label, &lv_mygec_font, LV_PART_MAIN);
    lv_obj_set_style_text_color(light_label, lv_color_hex(0X000000), 0);
    lv_obj_align(light_label, LV_ALIGN_TOP_LEFT, 30,180);
     lv_obj_set_style_text_font(light_label, &lv_mygec_font, 0);
    /* 退出按钮 */
    lv_obj_t* exit_tem_btn = lv_btn_create(main_cont);
    lv_obj_set_pos(exit_tem_btn, 600, 350);
    lv_obj_set_size(exit_tem_btn, 120, 100);
    lv_obj_set_style_bg_color(exit_tem_btn, lv_color_hex(0xFF0000), 0);
    lv_obj_set_style_text_color(exit_tem_btn, lv_color_white(), 0);

    lv_obj_t* exit_tem_label = lv_label_create(exit_tem_btn);
    lv_label_set_text(exit_tem_label, "退出");
     lv_obj_set_style_text_font(exit_tem_label, &lv_mygec_font, 0);
    lv_obj_add_event_cb(exit_tem_btn, exit_tem, LV_EVENT_CLICKED, NULL);
 
    lv_timer_t *ui_refresh_timer = lv_timer_create(ui_refresh_callback, 1000, NULL); // 创建一个定时器，每秒刷新一次UI

  
}



void update_tem_ui(void)
{
    if (main_cont == NULL) return;
    
    // 更新所有标签
  
    lv_label_set_text_fmt(temp_label, "温度: %d℃\n", Temp);
    
    /* 湿度标签 */
  
    lv_label_set_text_fmt(humi_label, "湿度: %d%%\n",hum);
  

    /* 气压标签 */
   
    lv_label_set_text_fmt(press_label, "气压: %dPa\n",press);
    
    /* 高度标签 */
   
    lv_label_set_text_fmt(alti_label, "海拔: %dm\n",hei);
    


    lv_label_set_text_fmt(light_label, "光照强度: %dlux\n",Lux);
    
   //lv_refr_now(NULL);
}




/*
    uart_init:      串口初始化
    @uart_name:     "/dev/ttySAC1" "/dev/ttySAC2" "/dev/ttySAC3"
    retval:         成功返回初始化好的串口文件描述符
                    失败返回 -1
*/
int uart_init(const char *uart_name)
{
	/*设置串口
	  波特率:9600
	  数据位:8
	  校验位:不要
	  停止位:1
	  数据流控制:无
	  */	
	int uart_fd= open(uart_name, O_RDWR);   //  打开串口设备文件
	if(uart_fd == -1)
	{
		perror("open error:");
		return -1;
	}

	struct termios myserial;
	//  清空结构体
	memset(&myserial, 0, sizeof (myserial));
	//  O_RDWR               
	myserial.c_cflag |= (CLOCAL | CREAD);
	//  设置控制模式状态，本地连接，接受使能
	//  设置 数据位
	myserial.c_cflag &= ~CSIZE;     //    清空数据位
	myserial.c_cflag &= ~CRTSCTS;   //  无硬件流控制
	myserial.c_cflag |= CS8;        //  数据位:8

	myserial.c_cflag &= ~CSTOPB;    //  1位停止位
	myserial.c_cflag &= ~PARENB;    //  不要校验

	cfsetospeed(&myserial, B9600);  //  设置波特率,B9600是定义的宏
	cfsetispeed(&myserial, B9600);

	/* 刷新输出队列,清除正接受的数据 */
	tcflush(uart_fd, TCIFLUSH);

	/* 改变配置 */
	tcsetattr(uart_fd, TCSANOW, &myserial);
	return uart_fd;
}


void * read_sensor_data(void *arg)
{
     // 1、初始化串口
    int com1_fd = uart_init("/dev/ttySAC2");
    if (com1_fd == -1) {
        fprintf(stderr, "Failed to initialize UART\n");
        return NULL;
    }

    unsigned char cmd1[3] = {0XA5,0X81,0X26}; // 获取光强值的命令
    unsigned char cmd2[3] = {0XA5,0X82,0X27}; // 获取其他值的命令
    unsigned char buf1[9] = {0};              // 用于保存光强值相关数据
    unsigned char buf2[15] = {0};             // 用于保存其他值相关数据
    
    while(1)
    {
        // 获取光强
        // 发送命令
        if (write(com1_fd, cmd1, sizeof(cmd1)) != sizeof(cmd1)) {
            perror("Failed to send cmd1");
            continue;
        }
        
        // 接收数据
        int r = read(com1_fd, buf1, sizeof(buf1));
        if (r != sizeof(buf1)) {
            printf("Incomplete read for cmd1, got %d bytes\n", r);
            //continue;
        }
        
        // 处理数据+显示数据
        if (buf1[0] != 0X5A || buf1[1] != 0X5A || buf1[2] != 0X15 || buf1[3] != 0X04) {
            printf("Invalid light data format\n");
            continue;
        }
        
         Lux = buf1[4] << 24 | buf1[5] << 16 | buf1[6] << 8 | buf1[7];
         Lux=(int)(Lux/100);
        printf("Lux = %dlux\n",Lux);

        // 获取其他数据
        // 发送命令
        if (write(com1_fd, cmd2, sizeof(cmd2)) != sizeof(cmd2)) {
            perror("Failed to send cmd2");
           // continue;
        }
        
        // 接收数据
        r = read(com1_fd, buf2, sizeof(buf2));
        if (r != sizeof(buf2)) {
            printf("Incomplete read for cmd2, got %d bytes\n", r);
            continue;
        }
        
        // 处理数据+显示数据
        if (buf2[0] != 0X5A || buf2[1] != 0X5A || buf2[2] != 0X45 || buf2[3] != 0X0A) {
            printf("Invalid sensor data format\n");
            continue;
        }
        
         Temp = buf2[4] << 8 | buf2[5];
         press = buf2[6] << 24 | buf2[7] << 16 | buf2[8] << 8 | buf2[9];
         hum = buf2[10] << 8 | buf2[11]; 
         hei = buf2[12] << 8 | buf2[13];
         Temp =(int)(Temp/100);
         press=(int)(press/100);
         hum=(int)(hum/100);
         hei=hei;
        printf("Temp = %d℃, hum = %d%%, Press = %d Pa, height = %dm\n", Temp, hum, press, hei);
        
        sleep(1);
    }
    
    // 3、关闭文件
    close(com1_fd);
    return NULL;
    }
   